package com.danger.Activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.danger.Activity.Account.ThemeActivity;
import com.danger.Activity.Account.UserInfoActivity;
import com.danger.Activity.Audio.AudioVideoListActivity;
import com.danger.Activity.Play.PlayVideoListActivity;
import com.danger.Activity.Setting.SettingActivity;
import com.danger.Model.bean.UserInfoBean;
import com.danger.MyApplication;
import com.danger.Presenter.IndexPresenter;
import com.danger.Presenter.IndexPresenterImpl;
import com.danger.Util.ImageUtil.BitmapBlur;
import com.danger.Util.ImageUtil.ImageLoaderUtil;
import com.danger.View.CommonView;
import com.danger.Widget.ImageCycleView;
import com.danger.Widget.MyCircleImageView;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import java.util.ArrayList;
import java.util.List;
import cn.bmob.v3.BmobUser;

public class IndexActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener, CommonView {

    private long exitTime = 0;
    private Toolbar toolbar;
    private NavigationView navigationView;
    private DrawerLayout drawer;
    private MyCircleImageView photo;
    private TextView username_textView;
    private LinearLayout head;
    private ImageCycleView index_imageCycleView;
    private List<ImageCycleView.ImageInfo> list;
    private RelativeLayout index_video_player_layout;
    private RelativeLayout index_video_layout;
    private RelativeLayout index_video_caption_layout;
    private RelativeLayout index_batch_task_layout;
    private ImageView index_video_caption_imageView;
    private ImageView index_video_imageView;
    private ImageView index_batch_task_imageView;
    private ImageView index_video_player_imageView;
    private TextView index_video_caption_textView;
    private TextView index_video_textView;
    private TextView index_batch_task_textView;
    private TextView index_video_player_textView;
    private IndexPresenter presenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_index);
        initView();
        initEvent();
    }

    @Override
    protected void onStart() {
        super.onStart();
        UserInfoBean user = BmobUser.getCurrentUser(this, UserInfoBean.class);
        username_textView.setText(user.getUsername().toString());
        ImageSize imageSize = new ImageSize(80, 80);
        if (user.getPhoto().toString() != null) {
            new ImageLoaderUtil(imageSize, photo, user.getPhoto().toString(), this).load();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        UserInfoBean user = BmobUser.getCurrentUser(this, UserInfoBean.class);
        username_textView.setText(user.getUsername().toString());
        ImageSize imageSize = new ImageSize(80, 80);
        if (user.getPhoto().toString() != null) {
            new ImageLoaderUtil(imageSize, photo, user.getPhoto().toString(), this).load();
        }
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        View navigationViewHeaderView = navigationView.getHeaderView(0);
        photo = (MyCircleImageView) navigationViewHeaderView.findViewById(R.id.imageView);
        username_textView = (TextView) navigationViewHeaderView.findViewById(R.id.username_textView);
        head = (LinearLayout) navigationViewHeaderView.findViewById(R.id.layout);
        head.setBackground(new BitmapDrawable(new BitmapBlur(this).blurBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.background))));
        index_imageCycleView = (ImageCycleView) findViewById(R.id.index_imageCycleView);
        list = new ArrayList<ImageCycleView.ImageInfo>();
        list.add(new ImageCycleView.ImageInfo(R.drawable.first, "广告位1", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.second, "广告位2", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.third, "广告位3", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.first, "广告位4", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.second, "广告位5", ""));
        index_video_player_layout = (RelativeLayout) findViewById(R.id.index_video_player_layout);
        index_video_layout = (RelativeLayout) findViewById(R.id.index_video_layout);
        index_video_caption_layout = (RelativeLayout) findViewById(R.id.index_video_caption_layout);
        index_batch_task_layout = (RelativeLayout) findViewById(R.id.index_batch_task_layout);
        index_video_caption_imageView = (ImageView) findViewById(R.id.index_video_caption_imageView);
        index_video_imageView = (ImageView) findViewById(R.id.index_video_imageView);
        index_batch_task_imageView = (ImageView) findViewById(R.id.index_batch_task_imageView);
        index_video_player_imageView = (ImageView) findViewById(R.id.index_video_player_imageView);
        index_video_caption_textView = (TextView) findViewById(R.id.index_video_caption_textView);
        index_video_textView = (TextView) findViewById(R.id.index_video_textView);
        index_batch_task_textView = (TextView) findViewById(R.id.index_batch_task_textView);
        index_video_player_textView = (TextView) findViewById(R.id.index_video_player_textView);
        UserInfoBean user = BmobUser.getCurrentUser(this, UserInfoBean.class);
        username_textView.setText(user.getUsername().toString());
        ImageSize imageSize = new ImageSize(80, 80);
        if (user.getPhoto().toString() != null) {
            new ImageLoaderUtil(imageSize, photo, user.getPhoto().toString(), this).load();
        }
        presenter = new IndexPresenterImpl(this, this);
        presenter.initSetting();
    }

    private void initEvent() {
        navigationView.setNavigationItemSelectedListener(this);
        index_imageCycleView.loadData(list, new ImageCycleView.LoadImageCallBack() {
            @Override
            public ImageView loadAndDisplay(ImageCycleView.ImageInfo imageInfo) {
                ImageView imageView = new ImageView(IndexActivity.this);
                imageView.setImageResource(Integer.parseInt(imageInfo.image.toString()));
                return imageView;
            }
        });
        index_video_player_layout.setOnClickListener(this);
        index_video_layout.setOnClickListener(this);
        index_video_caption_layout.setOnClickListener(this);
        index_batch_task_layout.setOnClickListener(this);
        photo.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.index, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.nav_my_source) {

        } else if (id == R.id.nav_theme) {
            startActivity(new Intent(IndexActivity.this, ThemeActivity.class));
        } else if (id == R.id.nav_my_calendar) {

        } else if (id == R.id.nav_setting) {
            startActivity(new Intent(IndexActivity.this, SettingActivity.class));
        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_feedback) {

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.index_video_player_layout:
                startActivity(new Intent(IndexActivity.this, PlayVideoListActivity.class));
                break;
            case R.id.index_video_layout:
                startActivity(new Intent(IndexActivity.this, AudioVideoListActivity.class));
                break;
            case R.id.index_video_caption_layout:
                break;
            case R.id.index_batch_task_layout:
                break;
            case R.id.imageView:
                startActivity(new Intent(IndexActivity.this, UserInfoActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void showSuccess(String message, String object) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                MyApplication.getInstance().exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void back() {

    }
}
